{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **Applio**\n",
    "A simple, high-quality voice conversion tool focused on ease of use and performance.\n",
    "\n",
    "[Support](https://discord.gg/urxFjYmYYh) — [GitHub](https://github.com/IAHispano/Applio)\n",
    "\n",
    "<br>\n",
    "\n",
    "### **Credits**\n",
    "- Encryption method: [Hina](https://github.com/hinabl)\n",
    "- Uv code: [Shirou](https://github.com/ShiromiyaG)\n",
    "- Filebrowser Login Password Fix & Tunnels code: [Nick088](https://linktr.ee/Nick088)\n",
    "- Main development: [Applio Team](https://github.com/IAHispano)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Install"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "trusted": true
   },
   "outputs": [],
   "source": [
    "import codecs\n",
    "from IPython.display import clear_output\n",
    "rot_47 = lambda encoded_text: \"\".join(\n",
    "    [\n",
    "        (\n",
    "            chr(\n",
    "                (ord(c) - (ord(\"a\") if c.islower() else ord(\"A\")) - 47) % 26\n",
    "                + (ord(\"a\") if c.islower() else ord(\"A\"))\n",
    "            )\n",
    "            if c.isalpha()\n",
    "            else c\n",
    "        )\n",
    "        for c in encoded_text\n",
    "    ]\n",
    ")\n",
    "\n",
    "new_name = rot_47(\"kmjbmvh_hg\")\n",
    "findme = rot_47(codecs.decode(\"pbbxa://oqbpcj.kwu/Dqlitvb/qurwg-mtnqvlmz.oqb\", \"rot_13\"))\n",
    "uioawhd = rot_47(codecs.decode(\"pbbxa://oqbpcj.kwu/QIPqaxivw/Ixxtqw.oqb\", \"rot_13\"))\n",
    "!pip install uv\n",
    "!git clone --depth 1 $uioawhd $new_name --branch 3.6.0\n",
    "clear_output()\n",
    "!apt update -y\n",
    "!apt install -y python3.11-dev portaudio19-dev psmisc\n",
    "!uv pip install -q -r /kaggle/working/program_ml/requirements.txt --extra-index-url https://download.pytorch.org/whl/cu128 --index-strategy unsafe-best-match --system\n",
    "%cd /kaggle/working/program_ml\n",
    "!python core.py \"prerequisites\" --models \"True\" --exe \"True\" --pretraineds_hifigan \"True\" > /dev/null 2>&1\n",
    "!sudo curl -fsSL https://raw.githubusercontent.com/filebrowser/get/master/get.sh | sudo bash\n",
    "!filebrowser config init\n",
    "!filebrowser config set --auth.method=noauth\n",
    "!filebrowser users add  \"applio\" \"applio123456\" --perm.admin\n",
    "clear_output()\n",
    "print(\"Finished\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Start\n",
    "\n",
    "### Tunnels Setup\n",
    "\n",
    "**WARNING:** Ngrok is currently the default tunnel option. Keep in mind that all free tunneling methods, including Ngrok, have limitations and may stop working unexpectedly from one day to the next.\n",
    "\n",
    "Select the Tunneling Service to Generate the Public URL:\n",
    "\n",
    "- Gradio + LocalTunnel: This will use Gradio for the Applio UI and LocalTunnel for the Tensorboard and Filebrowser. Select it in Tunnel, run the cell, wait for the Local URL to appear, open the Gradio Public URL for the Applio UI, instead for the Tensorboard and Filebrowser you need to copy the LocalTunnel Password and paste it in Tunnel Password of the LocalTunnel Tunnel Public URL. \n",
    "\n",
    "- Ngrok: Select it in Tunnel, get the Ngrok Tunnel Authtoken here: https://dashboard.ngrok.com/tunnels/authtokens/new, put it in ngrok_tunnel_authtoken, run the, wait for the Local URL to appear and click on the Ngrok Tunnel Public URL which is above.\n",
    "\n",
    "- LocalTunnel: Select it in Tunnel, run the cell, wait for the Local URL to appear, copy the LocalTunnel Password displayed under the public link and paste it in Tunnel Password of the LocalTunnel Tunnel Public URL which is above.\n",
    "\n",
    "- Horizon: Select it in Tunnel, get the Horizon ID here: https://hrzn.run/dashboard/, login, on the 2nd step, it shows an hrzn login YOUR_ID, you need to copy that id and put it in horizon_id. Then run the cell, you will get an 'HORIZON: Authorize at https://hrzn.run/dashboard/settings/cli-token-requests/YOUR_ID', click it, do Approve. At the end, run the cell, wait for the Local URL to appear and click on the Horizon Tunnel Public URL which is above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "trusted": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "import time\n",
    "import urllib.request\n",
    "from IPython.display import clear_output\n",
    "\n",
    "\n",
    "Tunnel = \"Ngrok\" #@param [\"Gradio + LocalTunnel\", \"Ngrok\", \"LocalTunnel\", \"Horizon\"]\n",
    "ngrok_authtoken = \"\" #@param {type:\"string\"}\n",
    "horizon_id = \"\" #@param {type:\"string\"}\n",
    "\n",
    "\n",
    "%cd /kaggle/working/program_ml\n",
    "os.system(f\"filebrowser -r /kaggle -p 9876 > /dev/null 2>&1 &\")\n",
    "%load_ext tensorboard\n",
    "%tensorboard --logdir logs --port 8077\n",
    "\n",
    "if Tunnel == \"Gradio + LocalTunnel\":\n",
    "  print(\"Using Gradio's built-in tunneling for Applio UI and LocalTunnel for Tensorboard and Filebrowser\")\n",
    "  # gradio  \n",
    "  share_option = \"--share\"\n",
    "  # localtunnel\n",
    "  # install localtunnel\n",
    "  !npm install -g localtunnel\n",
    "  import time\n",
    "  import urllib\n",
    "  # run localtunnel\n",
    "  # tensorboard\n",
    "  with open('t.txt', 'w') as file:\n",
    "    file.write('')\n",
    "\n",
    "  get_ipython().system_raw('lt --port 8077 >> t.txt 2>&1 &')\n",
    "\n",
    "  time.sleep(7)\n",
    "\n",
    "  endpoint_ip = urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip(\"\\n\")\n",
    "\n",
    "  with open('t.txt', 'r') as file:\n",
    "    t_tunnel = file.read()\n",
    "    t_tunnel = t_tunnel.replace(\"your url is: \", \"\")\n",
    "\n",
    "  # filebrowser\n",
    "  with open('f.txt', 'w') as file:\n",
    "    file.write('')\n",
    "\n",
    "  get_ipython().system_raw('lt --port 9876 >> f.txt 2>&1 &')\n",
    "\n",
    "  time.sleep(7)\n",
    "\n",
    "  endpoint_ip = urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip(\"\\n\")\n",
    "\n",
    "  with open('f.txt', 'r') as file:\n",
    "    f_tunnel = file.read()\n",
    "    f_tunnel = f_tunnel.replace(\"your url is: \", \"\")\n",
    "\n",
    "\n",
    "  clear_output()\n",
    "\n",
    "  print(f\"LocalTunnel Tensorboard Public URL: {t_tunnel}\")\n",
    "  print(f\"LocalTunnel Filebrowser Public URL: {f_tunnel}\")\n",
    "  print(f'LocalTunnels Password: {endpoint_ip}')\n",
    "elif Tunnel == \"Ngrok\":\n",
    "  !pip install -q pyngrok\n",
    "  from pyngrok import ngrok\n",
    "  ngrok.set_auth_token(ngrok_authtoken)\n",
    "  p_tunnel = ngrok.connect(6969)\n",
    "  t_tunnel = ngrok.connect(8077)\n",
    "  f_tunnel = ngrok.connect(9876)\n",
    "  clear_output()\n",
    "  print(f\"Applio Public URL: {p_tunnel.public_url}\")\n",
    "  print(f\"Tensorboard Public URL: {t_tunnel.public_url}\")\n",
    "  print(f\"FileBrowser Public URL: {f_tunnel.public_url}\")\n",
    "elif Tunnel == \"LocalTunnel\":\n",
    "  # install\n",
    "  !npm install -g localtunnel\n",
    "  import time\n",
    "  import urllib\n",
    "  # run localtunnel\n",
    "  # program_ml\n",
    "  with open('p.txt', 'w') as file:\n",
    "    file.write('')\n",
    "\n",
    "  get_ipython().system_raw('lt --port 6969 >> p.txt 2>&1 &')\n",
    "\n",
    "  time.sleep(7)\n",
    "\n",
    "  endpoint_ip = urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip(\"\\n\")\n",
    "\n",
    "  with open('p.txt', 'r') as file:\n",
    "    p_tunnel = file.read()\n",
    "    p_tunnel = p_tunnel.replace(\"your url is: \", \"\")\n",
    "\n",
    "  # tensorboard\n",
    "  with open('t.txt', 'w') as file:\n",
    "    file.write('')\n",
    "\n",
    "  get_ipython().system_raw('lt --port 8077 >> t.txt 2>&1 &')\n",
    "\n",
    "  time.sleep(7)\n",
    "\n",
    "  endpoint_ip = urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip(\"\\n\")\n",
    "\n",
    "  with open('t.txt', 'r') as file:\n",
    "    t_tunnel = file.read()\n",
    "    t_tunnel = t_tunnel.replace(\"your url is: \", \"\")\n",
    "\n",
    "  # filebrowser\n",
    "  with open('f.txt', 'w') as file:\n",
    "    file.write('')\n",
    "\n",
    "  get_ipython().system_raw('lt --port 9876 >> f.txt 2>&1 &')\n",
    "\n",
    "  time.sleep(7)\n",
    "\n",
    "  endpoint_ip = urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip(\"\\n\")\n",
    "\n",
    "  with open('f.txt', 'r') as file:\n",
    "    f_tunnel = file.read()\n",
    "    f_tunnel = f_tunnel.replace(\"your url is: \", \"\")\n",
    "\n",
    "\n",
    "  clear_output()\n",
    "\n",
    "  print(f\"Applio Public URL: {p_tunnel}\")\n",
    "  print(f\"Tensorboard Public URL: {t_tunnel}\")\n",
    "  print(f\"Filebrowser Public URL: {f_tunnel}\")\n",
    "  print(f'LocalTunnels Password: {endpoint_ip}')\n",
    "elif Tunnel == \"Horizon\":\n",
    "  # install \n",
    "  !npm install -g @hrzn/cli\n",
    "  import time\n",
    "  # login\n",
    "  !hrzn login $horizon_id\n",
    "  # run horizon\n",
    "  # program_ml\n",
    "  with open('p.txt', 'w') as file:\n",
    "    file.write('')\n",
    "\n",
    "  get_ipython().system_raw('hrzn tunnel http://localhost:6969 >> p.txt 2>&1 &')\n",
    "\n",
    "  time.sleep(7)\n",
    "\n",
    "  with open('p.txt', 'r') as file:\n",
    "    p_tunnel = file.read()\n",
    "    p_tunnel = !grep -oE \"https://[a-zA-Z0-9.-]+\\.hrzn\\.run\" p.txt\n",
    "    p_tunnel = p_tunnel[0]\n",
    "\n",
    "  # tensorboard\n",
    "  with open('t.txt', 'w') as file:\n",
    "    file.write('')\n",
    "\n",
    "  get_ipython().system_raw('hrzn tunnel http://localhost:8077 >> t.txt 2>&1 &')\n",
    "\n",
    "  time.sleep(7)\n",
    "\n",
    "  with open('t.txt', 'r') as file:\n",
    "    t_tunnel = file.read()\n",
    "    t_tunnel = !grep -oE \"https://[a-zA-Z0-9.-]+\\.hrzn\\.run\" t.txt\n",
    "    t_tunnel = t_tunnel[0]\n",
    "\n",
    "  # filebrowser\n",
    "  with open('f.txt', 'w') as file:\n",
    "    file.write('')\n",
    "\n",
    "  get_ipython().system_raw('hrzn tunnel http://localhost:9876 >> f.txt 2>&1 &')\n",
    "\n",
    "  time.sleep(7)\n",
    "\n",
    "  with open('f.txt', 'r') as file:\n",
    "    f_tunnel = file.read()\n",
    "    f_tunnel = !grep -oE \"https://[a-zA-Z0-9.-]+\\.hrzn\\.run\" f.txt\n",
    "    f_tunnel = f_tunnel[0]\n",
    "\n",
    "  clear_output()\n",
    "\n",
    "  print(f\"Applio Public URL: {p_tunnel}\")\n",
    "  print(f\"Tensorboard Public URL: {t_tunnel}\")\n",
    "  print(f\"FileBrowser Public URL: {f_tunnel}\")\n",
    "\n",
    "print(\"Save your links for later, this will take a while...\")\n",
    "!python app.py --client --host 0.0.0.0 --port 6969 $share_option\n",
    "\n",
    "# kills previously running processes\n",
    "!fuser -k 6969/tcp\n",
    "!fuser -k 8077/tcp\n",
    "!fuser -k 9876/tcp"
   ]
  }
 ],
 "metadata": {
  "kaggle": {
   "accelerator": "nvidiaTeslaT4",
   "dataSources": [],
   "dockerImageVersionId": 31090,
   "isGpuEnabled": true,
   "isInternetEnabled": true,
   "language": "python",
   "sourceType": "notebook"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
